package com.sky.controller.user;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.mybatis.mp.core.mybatis.mapper.context.Pager;
import com.sky.dto.BookRatingDTO;
import com.sky.dto.PageQueryDTO;
import com.sky.result.Result;
import com.sky.service.UserbookratingsService;
import com.sky.vo.BookRatingVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/bookrating")
@Api(tags = "用户图书评分接口")
@Slf4j
public class UserBookRatingController {

    @Autowired
    private UserbookratingsService userbookratingsService;

    /**
     * 添加图书评分
     */
    @SaCheckLogin
    @PostMapping
    @ApiOperation("添加图书评分")
    public Result<String> add(@RequestBody BookRatingDTO bookRatingDTO) {
        log.info("添加图书评分：{}", bookRatingDTO);
        userbookratingsService.addRating(bookRatingDTO);
        return Result.success();
    }

    /**
     * 修改图书评分
     */
    @SaCheckLogin
    @PutMapping
    @ApiOperation("修改图书评分")
    public Result<String> update(@RequestBody BookRatingDTO bookRatingDTO) {
        log.info("修改图书评分：{}", bookRatingDTO);
        userbookratingsService.updateRating(bookRatingDTO);
        return Result.success();
    }

    /**
     * 删除图书评分
     */
    @SaCheckLogin
    @DeleteMapping("/{ratingId}")
    @ApiOperation("删除图书评分")
    public Result<String> delete(@PathVariable Integer ratingId) {
        log.info("删除图书评分：{}", ratingId);
        userbookratingsService.deleteRating(ratingId);
        return Result.success();
    }

    /**
     * 获取图书的所有评分（分页）
     */
    @SaCheckLogin
    @PostMapping("/book/{bookId}")
    public Result<Pager<BookRatingVO>> getBookRatings(
            @PathVariable Integer bookId,
            @RequestBody PageQueryDTO pageQueryDTO) {
        log.info("分页获取图书评分，bookId：{}，pageQuery：{}", bookId, pageQueryDTO);
        Pager<BookRatingVO> pager = userbookratingsService.getBookRatings(
            bookId, 
            pageQueryDTO.getPage(), 
            pageQueryDTO.getPageSize()
        );
        return Result.success(pager);
    }

    /**
     * 获取用户的所有评分（分页）
     */
    @SaCheckLogin
    @PostMapping("/user/{userId}")
    public Result<Pager<BookRatingVO>> getUserRatings(
            @PathVariable Integer userId,
            @RequestBody PageQueryDTO pageQueryDTO) {
        log.info("分页获取用户评分，userId：{}，pageQuery：{}", userId, pageQueryDTO);
        Pager<BookRatingVO> pager = userbookratingsService.getUserRatings(
            userId, 
            pageQueryDTO.getPage(), 
            pageQueryDTO.getPageSize()
        );
        return Result.success(pager);
    }

    /**
     * 获取图书的平均评分
     */
    @SaCheckLogin
    @GetMapping("/average/{bookId}")
    public Result<Double> getAverageRating(@PathVariable Integer bookId) {
        Double averageRating = userbookratingsService.getAverageRating(bookId);
        return Result.success(averageRating);
    }
} 